<?php

// 平台
class PlantformAction extends Action {
	
	private $cLogs = null;
	
	private $cPlantform = null;
	
	function __construct(){

		//判断管理员登录状态
		admin_login_check();
		
		import('@.ORG.Get');
		import('@.ORG.Post');
		import('@.ORG.Plantform');
		import('@.ORG.Logs');
		
		$this->cPlantform = new Plantform();
		$this->cLogs = new Logs();
		
	}
	
	//列表
    public function index(){

    	admin_role('Plantform:index');

    	$plantforms = $this->cPlantform->lists();
       
    	$this->assign(
    		array(
    			'APP_NAME'		=> APP_NAME,
    			'plantforms'	=> $plantforms,
    			'MainPage'		=> 'Plantform:index'
    		)
    	);
    	$this->display(C('ADM_TEMPLATE'));
    }
    
    public function add(){

    	admin_role('Plantform:add');
    	
    	$this->assign(
    		array(
    			'APP_NAME'	=> APP_NAME,
    			'MainPage'	=> 'Plantform:add'
    		)
    	);
    	$this->display(C('ADM_TEMPLATE'));
    }
    
    public function add_do(){

    	admin_role('Plantform:add');
    	
   		$post = post(array(
    		array('plantform_name'		,'trim',''),
    		array('plantform_code'		,'trim',''),
    		array('plantform_url'		,'trim',''),
   			array('remark'				,'trim',''),
   			array('disabled'			,'intval','')
    	));

    	$add_result = $this->cPlantform->add($post);
    
    	if($add_result > 0){
    		
    		$this->cLogs->add('Plantform:add','添加平台成功:'.$post['plantform_name']);
    		
    		json_success('平台保存成功');
    	}else{
    		json_error('平台保存失败');
    	}
    }
    
    //详情或修改
    public  function detail(){
    	
    	admin_role('Plantform:edit');
    	
    	$get = get(array(
    		array('id','intval',0)
    		
    	));

    	$id = $get['id'];
    	
    	$plantform = $this->cPlantform->get_by_id($id);
    	
    	$this->assign(
    		array(
    			'APP_NAME'		=> APP_NAME,
    			'plantform'		=> $plantform,
    			'MainPage'		=> 'Plantform:detail'
    		)
    	);
    	
    	$this->display(C('ADM_TEMPLATE'));
    }
    
    public function update(){
    	
    	admin_role('Plantform:edit');
    	 
    	$post = post(array(
    		array('plantform_id'		,'intval',0),
    		array('plantform_name'		,'trim',''),
    		array('plantform_code'		,'trim',''),
    		array('plantform_url'		,'trim',''),
    		array('remark'				,'trim',''),
    		array('disabled'			,'intval','')
    	));
   	
    	$id = $post['plantform_id'];
    	
    	unset($post['plantform_id']);
    
    	$update_result = $this->cPlantform->update($id,$post);
    	
    	if($update_result){
    		
    		$this->cLogs->add('Plantform:edit','平台更新成功:'.$post['plantform_name']);
    		
    		json_success('保存成功');
    	}else{
    		json_error('保存失败');
    	}
    }
}